<?php

/**
 * 生成密码
 * @param type $password  明文密码
 * @return boolean
 */
function pwdbysalt($password) {
    if (empty($password)) {
        return false;
    } else {
        $salt = '$1$' . substr($password, 0, 4);
        return md5(crypt($password, $salt));
    }
}

/**
 * 通知（发布任务成功后想用户推送消息）
 * 获取用户openID、access_token等公用方法
 * $uids  分配用户组
 * $tid  任务ID
 */
function get_notice($uids, $tid) {
    if (empty($uids) || empty($tid)) {
        return FALSE;
    } else {
        $wchat = C('weixin');
        $appid = $wchat['Appid'];
        $secret = $wchat['AppSecret'];
        $json_token = http_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret);
        $access_token = json_decode($json_token, true);
//获得access_token
        $acc_t = $access_token[access_token];
        $task = M('task')->where('tid = ' . $tid)->find();
        $add = M('area')->where('id = ' . $task['province'])->getField('areaname') . " ";
        $add .= M('area')->where('id = ' . $task['city'])->getField('areaname') . " ";
        $add .= M('area')->where('id = ' . $task['district'])->getField('areaname') . " ";
        $tadd = $add . $task['address'];
        foreach ($uids as $k) {
            $info[$k] = M('users')->where('uid = ' . "'" . $k . "'")->find();
        }
        if (!empty($info)) {
            //echo $this->access_token;exit;
            //模板消息
            foreach ($info as $k => $val) {
                $res[$k]['res'] = get_res($val['appid'], $val['username'], $task['tname'], $tadd, $acc_t, $task['work_time'] . ' ' . $task['work_time_start'].':00');
            }
            if ($res[$k]['res'][errcode] == 0) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}

function get_res($openid, $uname, $tname, $tadd, $acc_t, $work_time) {
    $template = array(
        'touser' => $openid,
        'template_id' => "8_xZ9y4TKWaYJ8CRVMz8cbbJ-l-cF8q296BjEwvQ1Ss",
        'url' => "http://c.zhiguanyong.com/Api/User/JobAssign",
        'topcolor' => "#7B68EE",
        'data' => array(
            'first' => array('value' => urlencode($uname . "您好,您有新的任务"), 'color' => "#FF0000"),
            'keyword1' => array('value' => urlencode($tname), 'color' => '#1b325c'),
            'keyword2' => array('value' => urlencode($tadd), 'color' => '#1b325c'),
            'keyword3' => array('value' => urlencode($work_time), 'color' => '#1b325c'),
            //'keynote4' => array('value' => urlencode('这是测试'), 'color' => '#FF0000'),
            'remark' => array('value' => urlencode('接到提示后请按时到达工作地点'), 'color' => '#FF0000'),)
    );
    $json_template = json_encode($template);
    //echo $json_template;
    //echo $this->access_token;
    $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $acc_t;
    $res = http_request($url, urldecode($json_template));
    return $res;
}

function http_request($url, $data = array()) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    // POST数据
    curl_setopt($ch, CURLOPT_POST, 1);
    // 把post的变量加上
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}
